package com.piggy.settlement.sdk.client.labor.singlewithdraw;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.piggy.settlement.sdk.constant.PiggyLaborSingleWithdrawalConstants;
import com.piggy.settlement.sdk.util.v2.PiggyOpenBasicsVerifyUtil;

/**
 * @author ：daxia
 * @description：连续劳务单笔提现相关接口
 * @modified By：
 * @version: 1.0$
 */
public class PiggyLaborSingleWithdrawalApiClient {

    static PiggyLaborSingleWithdrawalApiClient piggySingleWithdrawalApiClient = null;

    private String appSecret = new String();
    private String domain = new String();

    public PiggyLaborSingleWithdrawalApiClient(String appSecret, String domain) {
        this.appSecret = appSecret;
        this.domain = domain;
    }

    public static PiggyLaborSingleWithdrawalApiClient getInstanse(String appSecret, String domain) {
        if (piggySingleWithdrawalApiClient == null) {
            piggySingleWithdrawalApiClient =
                    new PiggyLaborSingleWithdrawalApiClient(appSecret, domain);
        }

        return piggySingleWithdrawalApiClient;
    }

    /**
     * 单笔提现上报
     *
     * @param signleVo 参数实体：
     * @return
     */
    public JSONObject singleSubmit(Object signleVo) {

        try {
            String signStr = PiggyOpenBasicsVerifyUtil.getSigStr(appSecret, signleVo);

            JSONObject paramObj = JSONUtil.parseObj(signleVo);
            paramObj.putOpt("sign", signStr);

            String response = HttpRequest.post(domain + PiggyLaborSingleWithdrawalConstants.SINGLE_SUBMIT)
                    .body(paramObj.toString())
                    .timeout(20000)
                    .execute()
                    .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;
        } catch (Exception e) {
            System.out.println("单笔提现上报失败:");
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 单笔确认支付
     *
     * @param signleVo 参数实体：
     * @return
     */
    public JSONObject payConfirm(Object signleVo) {

        try {
            String signStr = PiggyOpenBasicsVerifyUtil.getSigStr(appSecret, signleVo);

            JSONObject paramObj = JSONUtil.parseObj(signleVo);
            paramObj.putOpt("sign", signStr);

            String response = HttpRequest.post(domain + PiggyLaborSingleWithdrawalConstants.PAY_CONFIRM)
                    .body(paramObj.toString())
                    .timeout(20000)
                    .execute()
                    .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;
        } catch (Exception e) {
            System.out.println("单笔确认支付失败:");
            e.printStackTrace();
            return null;
        }
    }
}
